/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package com.by.common.exception;

import com.by.common.bean.Result;
import com.by.common.controller.BaseController;
import com.mysql.cj.jdbc.exceptions.MysqlDataTruncation;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>Project: erp-cloud-api - GlobalExceptionHandler</p>
 * <p>Powered by yzc On 2024-01-08 19:33:54</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler extends BaseController {
    /**
     * 捕获后端验证异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ValidateException.class)
    public Result handlerValidateException(ValidateException e) {
        return new Result(0,"验证失败", e.getErrors());
    }

    /**
     * 数据太长
     * @param e
     * @return
     */
    @ExceptionHandler(MysqlDataTruncation.class)
    public Result handerDataTruncation(MysqlDataTruncation e){
        String msg=e.getMessage();
        int begin=msg.indexOf("'");
        int end=msg.lastIndexOf("'");
        String filed=msg.substring(begin+1,end);
        return new Result(0,"数据异常","属性["+filed+"]太长！");
    }


    @ExceptionHandler(FileSizeLimitExceededException.class)
    public Result handlerFileSizeLimitExceededException(FileSizeLimitExceededException e){
        return new Result(0,"文件大小超过10M",e.getMessage());
    }
}
